﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using RegExpressModel.Entities;


namespace RegExpressModel
{
	public class FindMatchesModel : BaseRegexModel
	{
		private string _input;


		public FindMatchesModel() : base()
        {
        	Matches = new List<SingleMatchResult>();
        }



		public string Input
		{
			get { return _input; }
			set
			{
				_input = value;
				ResetResults();
			}
		}


		public MatchResultType ResultStatus { get; private set; }
		public List<SingleMatchResult> Matches { get; private set; }



		public bool PerformMatch()
		{
			if(IsValid)
			{
				MatchCollection matches = RegexObject.Matches(Input);
				Matches.Clear();
				ResultStatus = (Matches.Count > 0)?
						MatchResultType.Matches : MatchResultType.NoMatch;

				foreach(Match match in matches)
				{
					SingleMatchResult result = new SingleMatchResult();
					result.SetResult(match, RegexObject);
					Matches.Add(result);
				}
				return true;
			}

			return false;
		}



		protected override void ResetResults()
		{
			ResultStatus = MatchResultType.Unknown;
			Matches.Clear();
		}

	
	}
}
